<?php

namespace app\controller;


use app\BaseController;
use app\dto\PageDto;
use app\dto\UserDto;
use app\model\Menu;
use app\model\ModifyQuery;
use app\model\User;
use app\model\BaseQuery;
use app\model\WenZhang;
use app\Request;
use app\service\RedisService;
use app\utils\Md5;
use app\utils\Utils;
use app\validate\WenzhangValidate;
use http\Message\Body;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Log;

function toUserDto(UserDto $data)
{
    return $data;
}

class MenuController extends BaseController
{


    function pageList(Request $request)
    {
        $menuDto = $request->all();
        $query = BaseQuery::build(Menu::class);
        if (Utils::isNotNull($menuDto,'id')) {
            $query->where('id', $menuDto['id']);
        }
        if (Utils::isNotNull($menuDto,'parentId')) {
            $query->where('parentId', $menuDto['parentId']);
        }
        if (Utils::isNotNull($menuDto,'name')) {
            $query->where('name','like', "{$menuDto['name']}%");
        }

        $resultlist = $query->select();
        $total = $query->count();
        return json([
            "data" => $resultlist,
            "count" => $total
        ]);
    }
    function getItem(Request $request)
    {
        $id = $request->get("id");
        $parentId = $request->get("parentId");
        $query = BaseQuery::build(Menu::class);
        if (isset($id)) {
            $query->where('id', $id);
        }
        if (isset($parentId)) {
            $query->where('$parentId', $parentId);
        }
        $resultlist = $query->find();
        return Utils::ok($resultlist);
    }

    function add(Request $request)
    {
        $params = $request->all();
        if(!isset($params)){
            return Utils::failRes("没有参数");
        }
        unset($params['id']);
        if (Utils::isNullPlus($params,'name')) {
            return Utils::failRes("没有参数,name");
        }
        if (Utils::isNullPlus($params,'funUrl')) {
            return Utils::failRes("没有参数,funUrl");
        }
        $id = Menu::save($params);
        return utils::ok($id);
    }

    function  update(Request  $request){
        $params=$request->all();
        /*校验 */
        if(Utils::isNullPlus($params,'id')){
            return Utils::failRes("id 必填");
        }
        /*查询是否存在*/
        $query = BaseQuery::build(Menu::class);
        $one = $query->where("id", $params['id']) -> find();
        if(Utils::isNullPlus($one,'id')){
            return Utils::failRes("id:{$params['id']} 没有找到数据");
        }
        $modifyQuery = ModifyQuery::build(Menu::class);
        $resultint=$modifyQuery->update($params,[
            'id'=>$params['id']
        ]);

        return Utils::ok($resultint);
    }

    function del(Request $request)
    {
        $id = $request->post("id");
        if (!isset($id)) {
            return Utils::failRes("id参数不能为空");
        }
        $userone = User::find($id);

        if (Utils::isNull($userone)) {
            return Utils::failRes("id: {$id} 找不到数据");
        }

        $result = BaseQuery::build(Menu::class)->where("id", $id)
            ->useSoftDelete('delete_time', Utils::dateyyyymmddhhmmss(time()))
            ->delete();
        return Utils::ok($result);
    }



}